package samfeng.erp.product.attribute.controller.admin;

import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.annotations.*;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;
import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;
import cn.iocoder.yudao.framework.operatelog.core.annotations.OperateLog;
import static cn.iocoder.yudao.framework.operatelog.core.enums.OperateTypeEnum.*;
import samfeng.erp.product.attribute.controller.admin.vo.*;
import samfeng.erp.product.attribute.convert.AttributeConvert;
import samfeng.erp.product.attribute.dal.dataobject.AttributeDO;
import samfeng.erp.product.attribute.service.AttributeService;

@Api(tags = "管理后台 - 产品属性")
@RestController
@RequestMapping("/product/attribute")
@Validated
public class AttributeController {

    @Resource
    private AttributeService attributeService;

    @PostMapping("/create")
    @ApiOperation("创建产品属性")
    @PreAuthorize("@ss.hasPermission('product:attribute:create')")
    public CommonResult<Long> createAttribute(@Valid @RequestBody AttributeCreateReqVO createReqVO) {
        return success(attributeService.createAttribute(createReqVO));
    }

    @PutMapping("/update")
    @ApiOperation("更新产品属性")
    @PreAuthorize("@ss.hasPermission('product:attribute:update')")
    public CommonResult<Boolean> updateAttribute(@Valid @RequestBody AttributeUpdateReqVO updateReqVO) {
        attributeService.updateAttribute(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @ApiOperation("删除产品属性")
    @ApiImplicitParam(name = "id", value = "编号", required = true, dataTypeClass = Long.class)
    @PreAuthorize("@ss.hasPermission('product:attribute:delete')")
    public CommonResult<Boolean> deleteAttribute(@RequestParam("id") Long id) {
        attributeService.deleteAttribute(id);
        return success(true);
    }

    @GetMapping("/get")
    @ApiOperation("获得产品属性")
    @ApiImplicitParam(name = "id", value = "编号", required = true, example = "1024", dataTypeClass = Long.class)
    @PreAuthorize("@ss.hasPermission('product:attribute:query')")
    public CommonResult<AttributeRespVO> getAttribute(@RequestParam("id") Long id) {
        AttributeDO attribute = attributeService.getAttribute(id);
        return success(AttributeConvert.INSTANCE.convert(attribute));
    }

    @GetMapping("/list")
    @ApiOperation("获得产品属性列表")
    @ApiImplicitParam(name = "ids", value = "编号列表", required = true, example = "1024,2048", dataTypeClass = List.class)
    @PreAuthorize("@ss.hasPermission('product:attribute:query')")
    public CommonResult<List<AttributeRespVO>> getAttributeList(@RequestParam("ids") Collection<Long> ids) {
        List<AttributeDO> list = attributeService.getAttributeList(ids);
        return success(AttributeConvert.INSTANCE.convertList(list));
    }

    @GetMapping("/page")
    @ApiOperation("获得产品属性分页")
    @PreAuthorize("@ss.hasPermission('product:attribute:query')")
    public CommonResult<PageResult<AttributeRespVO>> getAttributePage(@Valid AttributePageReqVO pageVO) {
        PageResult<AttributeDO> pageResult = attributeService.getAttributePage(pageVO);
        return success(AttributeConvert.INSTANCE.convertPage(pageResult));
    }

    @GetMapping("/export-excel")
    @ApiOperation("导出产品属性 Excel")
    @PreAuthorize("@ss.hasPermission('product:attribute:export')")
    @OperateLog(type = EXPORT)
    public void exportAttributeExcel(@Valid AttributeExportReqVO exportReqVO,
              HttpServletResponse response) throws IOException {
        List<AttributeDO> list = attributeService.getAttributeList(exportReqVO);
        // 导出 Excel
        List<AttributeExcelVO> datas = AttributeConvert.INSTANCE.convertList02(list);
        ExcelUtils.write(response, "产品属性.xls", "数据", AttributeExcelVO.class, datas);
    }

}
